﻿using Microsoft.Reporting.WinForms;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using SearchForWorkitemsChanges.ExternalActions.ExportXmlComponents;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SearchForWorkitemsChanges.ExternalActions.ExportRdlcComponents
{
    /// <summary>
    /// Windows used by Reporting plugin to display the report
    /// </summary>
    internal partial class WinReportViewer : Form
    {
        private string reportPath;
        private IEnumerable<SerializedFacadeWorkItem> srcWorkItems;

        public WinReportViewer(string reportPath, IEnumerable<SerializedFacadeWorkItem> src)
        {
            InitializeComponent();

            this.srcWorkItems = src;
            this.reportPath = reportPath;
        }

        internal const string RdlcReportUsed = "SearchForWorkitemsChanges.ExternalActions.Impl.ExportRdlcComponents.WorkItemReport.rdlc";
        private void WinReportViewer_Load(object sender, EventArgs e)
        {
            var reportDataSource = new ReportDataSource();

            if (String.IsNullOrEmpty(this.reportPath) || !File.Exists(this.reportPath))
            {
                this.reportContainer.LocalReport.ReportEmbeddedResource = RdlcReportUsed;
            }
            else 
            {
                this.reportContainer.LocalReport.ReportPath = new FileInfo(this.reportPath).FullName;
            }

            reportDataSource.Name = "SrcWorkItems";
            reportDataSource.Value = this.srcWorkItems;
            this.reportContainer.LocalReport.DataSources.Add(reportDataSource);

            this.reportContainer.RefreshReport();
        }
    }
}
